package cn.tedu.controller;



import cn.tedu.pojo.User;
import cn.tedu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/register")
    public String goToRegister(){
        return "/register.jsp";
    }

    @RequestMapping("/login")
    public String goTologin(){
        return "login";
    }

   /**
     * 用户登录验证
     *
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping("/userLoginVerify")
    public String userLoginVerify(String username, String password) {
        User user = userService.findUserByName(username);
        if (user == null) {
            return "1";
        } else {
            if (!user.getuPassword().equals(password)) {
                return "2";
            } else {
                return "ok";
            }
        }
    }

    /**
     * 跳转到用户主页
     *
     * @param username
     * @param session
     * @return
     */
    @RequestMapping("/login_action")
    public String goToHead(String username, HttpSession session) {
        User user = userService.findUserByName(username);
        if (user != null) {
            //将登录成功的用户存放到session中
            session.setAttribute("user", user);
            return "product_list_pageHelper";
        }
        return "login";
    }

    /**
     * 用户名是否存在
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping("/isUsernameExist")
    public String isUsernameExist(String username) {
        User user = userService.findUserByName(username);
        if (user != null) {
            return "此用户名太受欢迎,请更换一个";
        }
        return "ok";
    }

    /**
     * 用户注册验证码验证
     * @param authCode
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/userRegisterVerify")
    public String userRegisterVerify(String authCode, HttpSession session) {
        String randomCode = (String) session.getAttribute("randomCode");
        if (!authCode.equalsIgnoreCase(randomCode)) {
            return "验证码错误!";
        }
        return "ok";
    }

    /**
     * 注册新用户
     * @param username
     * @param uPassword
     * @param session
     * @return
     */
    @RequestMapping("/register_action")
    public String userRegister(String username, String uPassword,
                               HttpSession session) {
        //去除session中的验证码,防止二次注册
        session.removeAttribute("randomCode");
        if (!username.equals("") && !uPassword.equals("")){
            User user = new User();
            user.setuName(username).setuPassword(uPassword);
            session.setAttribute("user", user);
            userService.addUser(user);
            return "product_list_pageHelper";
        }
        return null;
    }

}
